package com.gdpu.controller;

import com.gdpu.api.CommonPage;
import com.gdpu.api.CommonResult;
import com.gdpu.dao.PortalOmsCartItemDao;
import com.gdpu.dao.PortalOmsOrderItemDao;
import com.gdpu.dto.OrderParam;
import com.gdpu.model.OmsCartItem;
import com.gdpu.model.OmsOrder;
import com.gdpu.service.OmsCartItemSerivece;
import com.gdpu.service.PortalOmsOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:weiming
 * @Date:2021/5/29
 */
@Api(tags = "订单模块")
@RestController
@RequestMapping("/orders")
public class PortalOmsOrderController {

    @Autowired
    private PortalOmsOrderService portalOmsOrderService;
    @Autowired
    private OmsCartItemSerivece omsCartItemSerivece;



    @ApiOperation("分页获取用户订单")
    @GetMapping ("/")
    CommonResult list(@RequestParam(name ="pageNum" ,defaultValue = "1") Integer pageNum,
                      @RequestParam(name ="pageSize" ,defaultValue = "10") Integer pageSize,
                      @RequestParam(name = "status",required = false) Integer status){
        CommonPage commonPage = portalOmsOrderService.get(status,pageNum,pageSize);
        HashMap<String, Object> map = new HashMap<>(1);
        map.put("orderList",commonPage);
        if (commonPage!=null){
            return CommonResult.success(map);
        }
        return  CommonResult.failed();
    }


    @ApiOperation("根据购物车id订单生成确认订单")
    @PostMapping ("/confirm")
    CommonResult generateConfirmOrder(@RequestBody List<Integer> cartItemIds){
        //返回总价
        Map map = portalOmsOrderService.generateConfirmOrder(cartItemIds);
        if (map!= null){
            return CommonResult.success(map);
        }

        return  CommonResult.failed();
    }

    @ApiOperation("订单生成")
    @PostMapping ("/generate")
    CommonResult generateOrder(@RequestBody OrderParam orderParam){

        Map<String, Object> map = portalOmsOrderService.generateOrder(orderParam);
        if (!map.isEmpty()){
            return CommonResult.success(map);
        }

        return CommonResult.failed();
    }

    @ApiOperation("支付")
    @PostMapping("/pay/{orderId}")
    CommonResult toPay(@PathVariable(name = "orderId") Integer orderId){
        int count = portalOmsOrderService.pay(orderId);
        if (count>0){
            return CommonResult.success("支付成功");
        }
        return  CommonResult.failed();
    }

    @ApiOperation("取消订单")
    @DeleteMapping("/{orderId}")
    CommonResult cancle(@PathVariable("orderId") Integer orderId){
        int count = portalOmsOrderService.cancel(orderId);
        if (count > 0){
            return  CommonResult.success("订单取消成功");
        }
        return CommonResult.failed();
    }

    @ApiOperation("订单详情")
    @GetMapping("/detail/{orderId}")
    CommonResult detail(@PathVariable("orderId") Integer orderId){
        Map map = portalOmsOrderService.detail(orderId);
        if (map != null){
            return  CommonResult.success(map);
        }
        return CommonResult.failed();
    }

    @ApiOperation("商品确认收货")
    @PostMapping("/confirm/{orderId}")
    CommonResult confirmGet(@PathVariable("orderId") Integer orderId){
        int count  = portalOmsOrderService.confirmGet(orderId);
        if (count != 0){
            return  CommonResult.success();
        }
        return CommonResult.failed();
    }


}
